from flask import Flask,render_template,request,redirect
from flask_sqlalchemy import SQLAlchemy
import json
app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///../stu.db'
db = SQLAlchemy(app)


class StuInfo(db.Model):
    sid = db.Column(db.String, primary_key=True)
    name = db.Column(db.String(80), unique=True)
    sex = db.Column(db.Integer,unique=True)
    class_name = db.Column(db.String(80), unique=True)
    phone = db.Column(db.String(11), unique=True)
    grade = db.Column(db.Integer, unique=True)
    major_name = db.Column(db.String(100), unique=True)
    college_name = db.Column(db.String(100), unique=True)
    email = db.Column(db.String(120), unique=True)
    password = db.Column(db.String(120), unique=True)
    desc = db.Column(db.String(255),unique=True)
loginUser = None
@app.route('/')
def hello_world():  # put application's code here
    return "Hello World Flask"
@app.route('/login')
def login():
    return render_template("index/login.html")
@app.route('/logout',methods=['post'])
def logout():
    global loginUser
    loginUser = None
    return {"code":200,"data":None,"msg":"退出成功"}

@app.route('/index')
def index():
    if loginUser is None:
        return redirect("/login")
    else:
        return render_template("index/index.html",currentUser = loginUser)
@app.route('/welcome')
def welcome():
    return render_template("index/welcome.html")

@app.route('/getAdminMenu')
def getAdminMenu():
    return render_template("index/menu1.html")
@app.route('/getFrontMenu')
def getFrontMenu():
    return render_template("index/menu2.html")

@app.route('/articleList')
def articleList():
    return render_template("index/article-list.html")
@app.route('/danyeList')
def danyeList():
    return render_template("index/danye-list.html")
@app.route('/stuInfoList')
def stuInfoList():
    return render_template("index/stuInfo-list.html")

@app.route('/personInfo')
def personInfo():
    sid = request.args.get('sid')
    stu = StuInfo.query.filter_by(sid=sid).first()
    return render_template("index/admin-info.html",userInfo = stu)

@app.route('/updateUserInfo',methods=['post'])
def updateUserInfo():
    sid = request.form.get("sid")
    name = request.form.get("name")
    email = request.form.get("email")
    desc = request.form.get("desc")
    update = StuInfo.query.get(sid)
    update.name = name
    update.email = email
    update.desc = desc
    db.session.commit()
    db.session.close()
    result = {'code': 200, 'data': None, 'msg': None}
    if update:
        result['code'] = 200
        result['msg'] = "修改成功"
    else:
        result['code'] = 201
        result['msg']="修改失败"
    return result
@app.route('/tologin',methods=['post'])
def tologin():
    global loginUser
    username = request.form.get("username")
    password = request.form.get("password")
    print(username,password)
    stu = StuInfo.query.filter_by(sid=username,password=password).first()
    result = {'code':200,'data':None,'msg':None}
    if stu:
        result['code'] = 200
        result['msg'] = "登录成功"
        loginUser = stu
    else:
        result['code'] = 201
        result['msg']="用户名或密码错误！"
    return result

@app.route('/getStuListLimit',methods=['post','get'])
def getStuListLimit():
    pass
if __name__ == '__main__':
    app.run()
